<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/7
 * Time: 12:19
 */
namespace Admin\Controller;

class ProductController extends CommonController{

    function _initialize()
    {
        parent::_initialize();
        $this->_product = D('Product');
    }
    // 产品首页
    public function index()
    {
        if(IS_POST){
            if($_POST['cate_id']!=0){
                $where['cate_id']=array('like',$_POST['cate_id']);
                $where['pro_title']=array('like','%'.$_POST['keywords'].'%');
            }
        }
        $count = $this->_product->where($where)->count();
        $page = new \Think\Page($count, 10); // 实例化分页类 条数，分页数
        $limit = $page->firstRow . ',' . $page->listRows;
        // relation('cate') 定义关联模型的某个表

        $this->cate = D('CateProduct')->getCategoryTree();
        $this->product = $this->_product->where($where)->relation(true)->limit($limit)->select();
        $this->page = $page->show();
        $this->display();
    }


    // 添加商品
    public function add(){
        if(IS_POST){
            $data =array(
                'cate_id'=>I('cate_id'),
                'pro_title'=>I('pro_title'),
                'pro_editor'=>I('pro_editor'),
                'pro_thumb'=>I('pro_thumb'),
                'pro_tag'=>I('pro_tag'),
                'pro_description'=>I('pro_description'),
                'pro_content'=>I('pro_content'),
                'pro_sale'=>I('pro_sale'),
                'pro_price'=>I('pro_price'),
                'pro_sn'=>I('pro_sn'),
                'pro_time'=>time()
            );
            $pro_thumb1 = I('pro_thumb1');
            $pro_thumb2 = I('pro_thumb2');
            $pro_thumb3 = I('pro_thumb3');
            $pro_thumb = '';
            if(!empty($pro_thumb1)) $pro_thumb=$pro_thumb1.',';
            if(!empty($pro_thumb2)) $pro_thumb.=$pro_thumb2.',';
            if(!empty($pro_thumb3)) $pro_thumb.=$pro_thumb3;


            // 验证通过 写入新增数据
            $res = $this->_product->add($data);
            $data_img=array(
                'pro_id'=>$res,
                'img_path'=>$pro_thumb,
                'img_time'=>time(),
            );
            if($res){
                if(!empty($pro_thumb)){
                    M('ProductImage')->add($data_img);
                }
                //卸载变量 用于 往CustomProduct表添加数据
                unset($_POST['cate_id']);
                unset($_POST['pro_title']);
                unset($_POST['pro_editor']);
                unset($_POST['pro_thumb']);
                unset($_POST['pro_tag']);
                unset($_POST['pro_description']);
                unset($_POST['pro_content']);
                unset($_POST['pro_sale']);
                unset($_POST['pro_price']);
                unset($_POST['pro_sn']);
                unset($_POST['pro_thumb1']);
                unset($_POST['pro_thumb2']);
                unset($_POST['pro_thumb3']);
                $_POST['pro_id']=$res;
                foreach($_POST as $key =>$val)
                {
                    //如果有数组及Checkbox类型的，进行序列化转换
                    if(is_array($val)){
                        $_POST[$key] = serialize($val);
                    }
                }
                M('CustomProduct')->add($_POST);
                $this->success('添加产品成功！', U(MODULE_NAME.'/Product/index'));
            }else{
                $this->error('添加产品失败！');
            }

        }else{
            $this->assign('field',fieldshow());
            $this->cate = D('CateProduct')->getCategoryTree();
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $pro_id = $_GET['pro_id'];
            $data =array(
                'cate_id'=>I('cate_id'),
                'pro_title'=>I('pro_title'),
                'pro_editor'=>I('pro_editor'),
                'pro_thumb'=>I('pro_thumb'),
                'pro_tag'=>I('pro_tag'),
                'pro_description'=>I('pro_description'),
                'pro_content'=>I('pro_content'),
                'pro_sale'=>I('pro_sale'),
                'pro_price'=>I('pro_price'),
                'pro_sn'=>I('pro_sn'),
                'pro_time'=>time()
            );
            if ($this->_product->where(array('pro_id' => $pro_id)) -> save($data)) {
                $pro_thumb1 = I('pro_thumb1');
                $pro_thumb2 = I('pro_thumb2');
                $pro_thumb3 = I('pro_thumb3');
                $pro_thumb = '';
                if(!empty($pro_thumb1)) $pro_thumb=$pro_thumb1.',';
                if(!empty($pro_thumb2)) $pro_thumb.=$pro_thumb2.',';
                if(!empty($pro_thumb3)) $pro_thumb.=$pro_thumb3;
                $data_img=array(
                    'img_path'=>$pro_thumb,
                    'img_time'=>time(),
                );
                if(!empty($pro_thumb)){
                    M('ProductImage')->where(array('pro_id'=>$pro_id))->save($data_img);
                }

                //卸载变量 用于 往CustomProduct表添加数据
                unset($_POST['cate_id']);
                unset($_POST['pro_title']);
                unset($_POST['pro_editor']);
                unset($_POST['pro_thumb']);
                unset($_POST['pro_tag']);
                unset($_POST['pro_description']);
                unset($_POST['pro_content']);
                unset($_POST['pro_sale']);
                unset($_POST['pro_price']);
                unset($_POST['pro_sn']);
                unset($_POST['pro_thumb1']);
                unset($_POST['pro_thumb2']);
                unset($_POST['pro_thumb3']);
                foreach($_POST as $key =>$val)
                {
                    //如果有数组及Checkbox类型的，进行序列化转换
                    if(is_array($val)){
                        $_POST[$key] = serialize($val);
                    }
                }
                M('CustomProduct')->where(array('pro_id'=>$pro_id))->save($_POST);

                $this -> success('更新产品成功！', U(MODULE_NAME . '/Product/index'));
            }else{
                $this->error('更新产品失败！');
            }

        }else{
            $pro_id = I('pro_id');
            $this->cate = D('CateProduct')->getCategoryTree();
            $this->field = $this->_product->find($pro_id);//查找当前ID数据
            $img = M('ProductImage')->field('img_path')->where(array('pro_id'=>$pro_id))->find();
            if(!empty($img)){
                $imgs=_arrayimage($img);
                $this->assign('imgs',$imgs);
            }

            $fis = M('CustomProduct')->where(array('pro_id'=>$pro_id))->find();
            foreach($fis as $k=>$v){
                if(is_serialized($v)){
                    $fis[$k]=unserialize($v);
                }
            }
            $this->assign('fields',edit_fieldshow($fis));

            $this->display();
        }
    }

    //删除文章
    public function destroy($pro_id){
        $res = $this->_product->where(array('pro_id'=>$pro_id))->delete();
        if($res){
            M('ProductImage')->where(array('pro_id'=>$pro_id))->delete();
            M('CustomProduct')->where(array('pro_id'=>$pro_id))->delete();
            $data =[
                'status'=>1,
                'info'=>'删除产品成功！',
            ];
        }else{
            $data = [
                'status'=>0,
                'info'=>'删除产品失败，请稍后再试！',
            ];
        }
        $this->ajaxReturn($data);
    }
    
    //批量删除文章
    public function deleteAll(){
        if(IS_POST){
            $artids = $_POST['pro_id'];
            $res = $this->_product->where(array('pro_id'=>array('in',$artids)))->delete();
            if($res){

                $data =[
                    'status'=>1,
                    'info'=>'批量删除产品成功！',
                ];
            }else{
                $data = [
                    'status'=>0,
                    'info'=>'批量删除产品失败，请稍后再试！',
                ];
            }
            $this->ajaxReturn($data);
        }
    }

    public function checkProductTitle() {
        if(!$_GET['pro_title']){
            echo json_encode(array("status" => 0, "info" => "标题为空"));
        }
        $where = "pro_title='" . I('get.title') . "'";
        if (!empty($_GET['pro_id'])) {
            $where.=" And pro_id !=" . (int) $_GET['pro_id'];
        }
        if(!I('get.pro_title')){
            echo json_encode(array("status" => 0, "info" => "请输入标题"));
        }
        if ($this->_product->where($where)->count() > 0) {
            echo json_encode(array("status" => 0, "info" => "已经存在，请修改标题"));
        } else {
            echo json_encode(array("status" => 1, "info" => "可以使用"));
        }
    }

}